package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {

    @Autowired
    private IAddressBookService addressBookService;

    @Override
    public List<AddressBookResDTO> getByUserIdAndCity(Long userId, String city) {

        List<AddressBook> addressBooks = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getCity, city)
                .list();
        if(CollUtils.isEmpty(addressBooks)) {
            return new ArrayList<>();
        }
        return BeanUtils.copyToList(addressBooks, AddressBookResDTO.class);
    }

    /**
     * 新增地址簿接口
     * @param addressBookUpsertReq
     * @return
     */
    @Override
    public void add(AddressBookUpsertReqDTO addressBookUpsertReq) {
        if (ObjectUtil.isEmpty(addressBookUpsertReq)){
            throw new BadRequestException("参数错误！！！");
        }
        AddressBook addressBook = new AddressBook();
        BeanUtil.copyProperties(addressBookUpsertReq,addressBook);
        // private Long userId;
        addressBook.setUserId(UserContext.currentUserId());
        // private Double lon;addressBookUpsertReq.getLocation= 200,400
        // 按逗号分割addressBookUpsertReq中的经纬度
        String[] split = addressBookUpsertReq.getLocation().split(",");
        addressBook.setLon(Double.valueOf(split[0]));
        // private Double lat;
        addressBook.setLat(Double.valueOf(split[1]));

        addressBookService.save(addressBook);
    }

    /**
     * 地址簿分页查询接口
     *
     * @param
     * @return
     */
    @Override
    public PageResult<AddressBook> pageQury(AddressBookPageQueryReqDTO addressBookPageQueryReq) {
        Page<AddressBook> addressBookPage = PageUtils.parsePageQuery(addressBookPageQueryReq, AddressBook.class);
        LambdaQueryWrapper<AddressBook> queryWrapper = Wrappers.<AddressBook>lambdaQuery().eq(AddressBook::getIsDeleted, 0);
        Page<AddressBook> serveTypePage = baseMapper.selectPage(addressBookPage,queryWrapper);

        return PageUtils.toPage(serveTypePage, AddressBook.class);
    }
}
